# Copyright (C) 2017-2022  CEA/DEN, EDF R&D
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#
# Author : Anthony Geay (EDF R&D)

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

ADD_DEFINITIONS(${PYTHON_DEFINITIONS} ${NUMPY_DEFINITIONS} ${SCIPY_DEFINITIONS} -DMEDCOUPLING_GIT_SHA1=\"${MEDCOUPLING_GIT_SHA1}\" -DMEDCOUPLING_CONFIG_DT=\"${MEDCOUPLING_CONFIG_TIME}\")

SET_SOURCE_FILES_PROPERTIES(medcoupling.i PROPERTIES CPLUSPLUS ON)
IF(WIN32)
  SET_PROPERTY(SOURCE medcoupling.i PROPERTY COMPILE_DEFINITIONS WIN32)
ENDIF()
IF ("${PYTHON_VERSION_MAJOR}" STREQUAL "3")
  SET_SOURCE_FILES_PROPERTIES(medcoupling.i PROPERTIES SWIG_FLAGS "-py3")
ELSE()
  SET_SOURCE_FILES_PROPERTIES(medcoupling.i PROPERTIES SWIG_DEFINITIONS "-shadow")
ENDIF()
SET(SWIG_MODULE_medcoupling_EXTRA_FLAGS "${NUMPY_DEFINITIONS};${SCIPY_DEFINITIONS}")
IF(MEDCOUPLING_USE_64BIT_IDS)
  STRING(APPEND SWIG_MODULE_medcoupling_EXTRA_FLAGS ";-DMEDCOUPLING_USE_64BIT_IDS")
ENDIF(MEDCOUPLING_USE_64BIT_IDS)

SET(medcoupling_SWIG_DPYS_FILES medcoupling.i)

INCLUDE_DIRECTORIES(
  ${PYTHON_INCLUDE_DIRS}
  ${NUMPY_INCLUDE_DIR}
  ${MEDFILE_INCLUDE_DIRS}
  ${HDF5_INCLUDE_DIRS}
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}/../MEDLoader
  ${CMAKE_CURRENT_SOURCE_DIR}/../MEDLoader/Swig
  ${CMAKE_CURRENT_SOURCE_DIR}/../MEDCoupling_Swig
  ${CMAKE_CURRENT_SOURCE_DIR}/../MEDCoupling
  ${CMAKE_CURRENT_SOURCE_DIR}/../INTERP_KERNEL
  ${CMAKE_CURRENT_SOURCE_DIR}/../INTERP_KERNEL/Bases
  ${CMAKE_CURRENT_SOURCE_DIR}/../INTERP_KERNEL/Geometric2D
  ${CMAKE_CURRENT_SOURCE_DIR}/../INTERP_KERNEL/ExprEval
  ${CMAKE_CURRENT_SOURCE_DIR}/../INTERP_KERNEL/GaussPoints
  ${CMAKE_CURRENT_SOURCE_DIR}/../RENUMBER
  ${CMAKE_CURRENT_SOURCE_DIR}/../RENUMBER_Swig
  ${CMAKE_CURRENT_SOURCE_DIR}/../MEDPartitioner
  ${CMAKE_CURRENT_SOURCE_DIR}/../MEDPartitioner_Swig
  ${CMAKE_CURRENT_SOURCE_DIR}/../ICoCo
  ${CMAKE_CURRENT_SOURCE_DIR}/../ICoCo/Swig
  ${CMAKE_CURRENT_SOURCE_DIR}/../ParaMEDMEM
  ${CMAKE_CURRENT_SOURCE_DIR}/../ParaMEDMEM/MPIAccess
  ${CMAKE_CURRENT_SOURCE_DIR}/../ParaMEDMEM_Swig
  ${PROJECT_BINARY_DIR}/doc
  )

IF(WIN32 OR CYGWIN)
  SET(medcoupling_LIB_dependancies ${PYTHON_LIBRARIES} ${PLATFORM_LIBS} medcouplingremapper medicoco)
ELSE(WIN32)
  # ${PYTHON_LIBRARIES} not needed see https://www.python.org/dev/peps/pep-0513/#libpythonx-y-so-1
  SET(medcoupling_LIB_dependancies ${PLATFORM_LIBS} medcouplingremapper medicoco)
ENDIF(WIN32)
  
IF(NOT MEDCOUPLING_MICROMED)
  LIST(APPEND SWIG_MODULE_medcoupling_EXTRA_FLAGS -DWITH_MED_FILE)
  LIST(APPEND medcoupling_LIB_dependancies medloader)

  IF(MEDCOUPLING_ENABLE_RENUMBER)
    LIST(APPEND SWIG_MODULE_medcoupling_EXTRA_FLAGS -DWITH_RENUMBER)
    LIST(APPEND medcoupling_LIB_dependancies renumbercpp)
  ENDIF(MEDCOUPLING_ENABLE_RENUMBER)
  
  IF(MEDCOUPLING_ENABLE_PARTITIONER)
    LIST(APPEND SWIG_MODULE_medcoupling_EXTRA_FLAGS -DWITH_PARTITIONER)
    LIST(APPEND medcoupling_LIB_dependancies medpartitionercpp)
  ENDIF(MEDCOUPLING_ENABLE_PARTITIONER)
ENDIF(NOT MEDCOUPLING_MICROMED)

IF(MEDCOUPLING_USE_MPI)
  INCLUDE_DIRECTORIES(${MPI_INCLUDE_DIRS})
  ADD_DEFINITIONS(${MPI_DEFINITIONS})
  LIST(APPEND SWIG_MODULE_medcoupling_EXTRA_FLAGS -DWITH_PARALLEL_INTERPOLATOR)
  LIST(APPEND medcoupling_LIB_dependancies paramedmem)
ENDIF(MEDCOUPLING_USE_MPI)

IF(${CMAKE_VERSION} VERSION_LESS "3.8.0")     
  SWIG_ADD_MODULE(medcoupling python medcoupling.i)
ELSE()
  SWIG_ADD_LIBRARY(medcoupling LANGUAGE python SOURCES medcoupling.i)
ENDIF()

SWIG_LINK_LIBRARIES(medcoupling ${medcoupling_LIB_dependancies})
SWIG_CHECK_GENERATION(medcoupling)
IF(WIN32)
  SET_TARGET_PROPERTIES(_medcoupling PROPERTIES DEBUG_OUTPUT_NAME _medcoupling_d)
  # To increase the size of the .obj file on Windows because MEDLoaderPYTHON_wrap.cxx, generated by SWIG, is too big
  TARGET_COMPILE_OPTIONS(_medcoupling PRIVATE /bigobj) 
ENDIF(WIN32)

INSTALL(TARGETS _medcoupling DESTINATION ${MEDCOUPLING_INSTALL_PYTHON})
INSTALL(FILES medcoupling.i medcoupling_pycode DESTINATION ${MEDCOUPLING_INSTALL_HEADERS})
SALOME_INSTALL_SCRIPTS(${CMAKE_CURRENT_BINARY_DIR}/medcoupling.py ${MEDCOUPLING_INSTALL_PYTHON} EXTRA_DPYS "${SWIG_MODULE_medcoupling_REAL_NAME}")


IF(MEDCOUPLING_BUILD_PY_TESTS)
    INCLUDE(tests.set)
ENDIF()

SALOME_GENERATE_TESTS_ENVIRONMENT(tests_env)

FOREACH(test ${BASE_TESTS})
  GET_FILENAME_COMPONENT(testname ${test} NAME_WE)
  ADD_TEST(NAME ${testname} COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/${test})
  SET_TESTS_PROPERTIES(${testname} PROPERTIES ENVIRONMENT "${tests_env}")
ENDFOREACH()

SET(TEST_INSTALL_DIRECTORY ${MEDCOUPLING_INSTALL_TESTS}/PyWrapping)

INSTALL(FILES CTestTestfileInstall.cmake DESTINATION ${TEST_INSTALL_DIRECTORY} RENAME CTestTestfile.cmake)
INSTALL(FILES ${BASE_TESTS} DESTINATION ${TEST_INSTALL_DIRECTORY})
INSTALL(FILES tests.set DESTINATION ${TEST_INSTALL_DIRECTORY})
